<?php
namespace Admin\Controller;

// +----------------------------------------------------------------------
// | 文件 CircleController.class.php
// +----------------------------------------------------------------------
// | 说明 圈子管理
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------
// | Author: 谢赤心 Legend. <xcx_legender@qq.com>
// +----------------------------------------------------------------------

class ThreadsController extends BaseManageController {
    /**
    *控制操作的模型表
    */
    protected $modelName = 'Threads'; 

    /**
    *删除类型的设置 true|直接删除， false|设置数据状态
    */
    protected $is_del   = false;

    /**
    *跳转的Url
    */
    // protected $jumpAct  = 'Threads';

    protected $text_map = array(
        'status' => array(0 => '不显示', '1' => '显示'),
        'attachment_type' => ['文字', '图文', '视频'],
    ); // 字符和文字的映射关系

    /**
    *操作index的前置操作
    *---设置数据筛选条件---
    */
    public function _before_index()
    {
        $circle_id = I('circle_id', 0, 'intval');
        if (!$circle_id){
            $this->error('缺少circle_id');
        }

        $circle = Service('Circle')->find($circle_id);
        $this->assign('circle', $circle);
        $args = ['circle_id'  => $circle_id];
        $map = array(
            'circle_id'  => $circle_id,
            'is_top'     => 0,
            'deleted_at' => ['$eq' => null],
        );//筛选条件

        $search = trim(I('get.title', '', 'strip_tags'));
        if($search){
            $map['title']    =   array('like', (string)$search);
            $args['title'] = I('title');
        }

        if (I('uid')){
            $args['uid'] = $map['uid'] = I('uid', 0, 'intval');
        }

        if (I('attachment_type')){
            $args['attachment_type'] = $attachment_type = I('attachment_type');
            $map['attachment_type'] = $attachment_type == 'null' ? ['$eq' => null]: I('attachment_type', 0, 'intval');
            $this->assign('attachment_type_text', $attachment_type == 'null' ? $this->text_map['attachment_type'][0] : $this->text_map['attachment_type'][$attachment_type]); 
        }

        if (isset($_GET['status'])){
            $map['status'] = I('status', 0, 'intval');
            $args['status'] = I('status');
        }

        $this->where = $map;

        $top_list = service('Threads')->where(['circle_id' => $circle_id, 'is_top' => 1, 'deleted_at' => ['$eq' => null]])->select();
        int_to_string($top_list, $this->text_map);
        $this->format_lists($top_list);

        $this->assign('top_list', $top_list);
        $this->assign('args', http_build_query($args));


        $this->assign('circle_id', $circle_id);
    }


    public function _before_add()
    {
        $circle_id = I('circle_id', 0, 'intval');
        if (!$circle_id){
            $this->error('缺少circle_id');
        }
        $this->assign('circle_id', $circle_id);
        if (IS_POST){
            $this->parsePost();
        }
    }


    public function _before_edit()
    {
        $this->assign('_id', I('id'));
        if (IS_POST){
            $this->parsePost();
        }
    }

    public function parsePost()
    {

        if (I('circle_id') == 1 && !I('thumb')){
            $this->error('官方话题必须上传缩略图!');
        }

        if (!I('attachment_type')){
                unset($_POST['attachment_type']);
            }

            if (I('attachment_type') == 1){
                 !I('pics') && $this->error('请上传图片');
                 $pics = array_filter(I('pics'));
                 $_POST['pics'] = [];
                 $scales = array_filter(I('scale'));
                 if ( count($pics) != count($scales)){
                    $this->error('图片信息错误, 请刷新重新上传...');
                 }

                 foreach ($pics as $key => $pic) {
                    $info = pathinfo($pic);
                    $ext  = $info['extension'];
                    $picsFormat[] = [
                        'url'   => url_format($pic),
                        'scale' => $scales[$key],
                        'mime'  => 'image/'.$ext,
                    ];
                 }
                 $_POST['pics'] = $picsFormat;
            }

            if (I('attachment_type') == 2){
                !I('video_id') && $this->error('请填写视频ID');
                $video_id = I('video_id', 0, 'intval');
                $video = service('video')->where(['id' => $video_id])->find();
                !$video && $this->error('视频信息错误');
                $video_sim = [
                    'id'    => $video_id,
                    'title' => $video['title'],
                    'detail' => $video['detail'],
                    'thumb'  => url_format($video['thumb']),
                ];
                if ($video['dpi_width'] && $video['dpi_height']){
                    $video_sim['scale'] = $video['dpi_width'].':'.$video['dpi_height'];
                }

                $_POST['video'] = $video_sim;
            }
            // print_r($_POST);
            unset($_POST['video_id']);
            unset($_POST['scale']);

            $uid = $_POST['uid'];
            if (!$uid){
                $this->error('请填写前台用户');
            }

            $user = event('User')->info($uid);
            if (!$user || empty($user['status'])){
                $this->error('用户不存在或已删除');
            }

    }


    public function format_lists(&$lists) {
        foreach ($lists as $_id => &$item) {
            $item['floor'] = service('Posts')->getFloor($_id);
        }
    }


    public function top($type, $id)
    {
        $threads = service('Threads')->find($id);
        if ($threads){
            $save['is_top'] = $type == 'up' ? 1:0;
            $save['_id'] = $id;
            service('Threads')->save($save);
            $this->success('操作成功');
        }else{
            $this->error('error');
        }
    }


}


